package com.starblink.search.category.ui.newlist

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.LinearLayoutManager
import com.starblink.android.basic.adapter.MutableAdapter
import com.starblink.android.basic.base.fragment.BaseTVMFragment
import com.starblink.android.basic.brv.DefaultDecoration
import com.starblink.android.basic.extension.dp2px
import com.starblink.android.basic.extension.lazyOnNone
import com.starblink.basic.route.RoutePage
import com.starblink.search.category.data.CategoryWithParentId
import com.starblink.search.category.ui.newlist.cell.NewLevelTwoCell
import com.starblink.search.databinding.FragmentCategoryChildrenBinding
import com.starblink.search.databinding.ItemTwoCategoryBinding

class CategoryChildrenFragment :
    BaseTVMFragment<FragmentCategoryChildrenBinding, CategoryChildrenVM>() {
    var dataList: HashMap<String, List<CategoryWithParentId>> = hashMapOf()

    companion object {
        fun newInstance(categoryId: String?) = CategoryChildrenFragment().apply {
            arguments = bundleOf(
                RoutePage.ARG.ID to categoryId
            )
        }
    }

    private val mAdapter: MutableAdapter by lazyOnNone {
        MutableAdapter(viewModel.contentLD.value!!).apply {
            addVhDelegate {
                NewLevelTwoCell(
                    mContext,
                    ItemTwoCategoryBinding.inflate(layoutInflater, it, false),
                ) { position, snapshot ->

                    if (viewModel.contentLD.value!![position].category.children.isNullOrEmpty()) {
                        viewModel.getSalesZoneLevel3ById(
                            isInit = false,
                            viewModel.contentLD.value!![position].category.id,
                            position,
                            snapshot
                        )
                    } else {
                        //这里不全部置为false
//                        viewModel.contentLD.value!!.forEach { item ->
//                            item.expand = false
//                        }
                        viewModel.contentLD.value!![position].expand = !snapshot
                        notifyDataSetChanged()
                    }
                }
            }
        }
    }

    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentCategoryChildrenBinding {
        return FragmentCategoryChildrenBinding.inflate(inflater, container, false)
    }

    override fun startObserve() {
        viewModel.contentLD.observe(this) {
            if (it.isNotEmpty()) {
                if (!dataList.containsKey(CategoryConfig.getCategoryId())) {
                    val list = ArrayList<CategoryWithParentId>(it)
                    CategoryConfig.getCategoryId().let { it1 -> dataList.put(it1, list) }
                }
                mAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun initData() {
        viewBinding.recycler.layoutManager = LinearLayoutManager(mActivity)
        viewBinding.recycler.addItemDecoration(DefaultDecoration(mActivity).apply {
            startVisible = false
            endVisible = true
            this.setDivider(10.dp2px())
        })
        viewBinding.recycler.adapter = mAdapter


        CategoryConfig.setCategoryIdChangeListener(object :
            CategoryConfig.CategoryIdChangeListener {
            override fun change(id: String) {
                if (dataList.containsKey(id)) {
                    val list: List<CategoryWithParentId> = dataList[id]!!
                    viewModel.changeData(list)
                } else {
                    viewModel.loadIniData()
                }
            }
        })
    }


}